1 Processamento dos dados

source("./load_files.R")
data_files_selected <- 
  list.files(
    pattern = ".*rafael.*resultado.*csv$",
    recursive = TRUE,
    ignore.case = TRUE
    )

data_files_and_size <- 
  sapply(data_files_selected, file.size)

files_to_include_in_dataframe <- 
  tibble(
    "Files" = names(data_files_and_size),
    "Size (in MB)" = data_files_and_size/1E6
   )

skimr::skim(files_to_include_in_dataframe)
Data summary
Name files_to_include_in_dataf…
Number of rows 2
Number of columns 2
_______________________
Column type frequency:
character 1
numeric 1
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
Files 0 1 92 94 0 2 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Size (in MB) 0 1 0.87 0.49 0.52 0.69 0.87 1.04 1.21 ▇▁▁▁▇
cdr <- load_cdr(names(data_files_and_size))

cdr %<>% 
  mutate(
    expgroup = case_when(
      str_detect(file, "rafael") ~ "rafael",
      TRUE ~ "unknown"),
    cycle = case_when(
      str_detect(file, "R0") ~ "R0",
      str_detect(file, "R4") ~ "R4",
      TRUE ~ "unknown"),
    time = case_when(
        str_detect(file, "Initial") ~ "initial",
        str_detect(file, "Final") ~ "final",
        TRUE ~ "unknown")) %>% 
  select(cdr3, cycle, time, expgroup, everything())

cdr %<>% 
  group_by(cdr3, expgroup) %>% 
  arrange(cycle, desc(time), .by_group = TRUE) %>% 
  mutate(
    fcp = cdrp / lag(cdrp, default = first(cdrp)),
    fcq = quantity / lag(quantity, default = first(quantity))
  ) %>% 
  select(cdr3:quantity, fcp, fcq, everything())
cdr %>% 
  filter(time == "final") %>% 
  group_by(expgroup, cycle, time) %>% 
  arrange(desc(fcp)) %>% 
  slice_head(prop = .1) %>% 
  # slice_head(n = 1000) %>% 
  ggplot(aes(expgroup, log10(fcp))) +
    geom_violin(aes(fill = expgroup, color = expgroup), alpha = 0.5) +
    geom_jitter(aes(shape = expgroup), alpha = 0.6, size = 1) +
    stat_summary(
      fun = mean,
      fun.min = mean,
      fun.max = mean,
      geom = "crossbar",
      # width = 0.5,
      aes(color = expgroup)
    ) +
    facet_grid(. ~ cycle)

# cdr %>% 
#   filter(str_detect(cycle, "R0")) %>% 
#   filter(time == "final") %>% 
#   group_by(expgroup, cycle, time) %>% 
#   arrange(desc(fcp)) %>% 
#   slice_head(n = 1000) %>% 
#   ggplot(aes(fcp, color = expgroup, fill = expgroup)) +
#     geom_density(stat = "bin", alpha = 0.3) +
#     facet_grid(cycle ~ expgroup)

cdr %<>% 
  group_by(expgroup, cycle, time) %>% 
  arrange(desc(fcp)) %>% 
  slice_head(prop = .1) %>% 
  mutate(
    threshold = mean(log10(fcp))
    ) %>% 
  mutate(
    rich = if_else(
        (log10(fcp) >= threshold) &
          # (time == "final") &
          # (str_detect(cycle, "R0")),
          (time == "final"),
      "rich",
      "medium")) %>% 
  full_join(cdr) %>% 
  mutate(
    rich = if_else(
      is.na(rich),
      "poor",
      rich)) %>% 
  mutate(
    rich = factor(rich,
                  levels = c("rich", "medium", "poor"))
    ) %>% 
  mutate(
    threshold = if_else(
      is.na(threshold),
      0,
      threshold
    )
  )

cdr %>% 
  filter(rich == "rich") %>%
  ggplot() +
    geom_violin(aes(expgroup, log10(fcp), fill = expgroup)) +
    geom_jitter(aes(expgroup, log10(fcp), shape = expgroup), alpha = .2) +
    facet_grid(rich ~ cycle)

cdr %>% 
  filter(!rich == "rich") %>%
  ggplot() +
    geom_violin(aes(expgroup, log10(fcp), fill = expgroup)) +
    geom_jitter(aes(expgroup, log10(fcp), shape = expgroup), alpha = .2) +
    facet_grid(rich ~ cycle)

names(cdr)
##  [1] "cdr3"      "cycle"     "time"      "expgroup"  "cdrp"      "quantity" 
##  [7] "fcp"       "fcq"       "length"    "MW"        "AV"        "IP"       
## [13] "flex"      "gravy"     "SSF_Helix" "SSF_Turn"  "SSF_Sheet" "n_A"      
## [19] "n_C"       "n_D"       "n_E"       "n_F"       "n_G"       "n_H"      
## [25] "n_I"       "n_K"       "n_L"       "n_M"       "n_N"       "n_P"      
## [31] "n_Q"       "n_R"       "n_S"       "n_T"       "n_V"       "n_W"      
## [37] "n_Y"       "aliphatic" "aromatic"  "neutral"   "positive"  "negative" 
## [43] "invalid"   "file"      "threshold" "rich"
cdr %>% 
  ggplot() +
    geom_density(aes(AV, color = rich)) +
    facet_grid(cycle ~ .)

cdr %>% 
  ggplot() +
    geom_density(aes(MW, color = rich)) +
    facet_grid(cycle ~ .)

cdr %>% 
  ggplot() +
    geom_density(aes(gravy, color = rich)) +
    facet_grid(cycle ~ .)

cdr %>% 
  ggplot() +
    geom_density(aes(SSF_Turn, color = rich)) +
    facet_grid(cycle ~ .)

cdr %>% 
  ggplot() +
    geom_density(aes(IP, color = rich)) +
    facet_grid(cycle ~ .)

cdr %>% 
  ggplot() +
    geom_density(aes(flex, color = rich)) +
    facet_grid(cycle ~ .)

cdr %>% 
  ggplot(aes(cycle, MW)) +
    geom_violin(aes(fill = rich)) +
    geom_jitter(alpha = .1) +
    facet_grid(rich ~ cycle)

cdr %>% 
  filter(rich == "rich") %>% 
  ungroup() %>% 
  select(cdr3:SSF_Sheet & !c("cycle", "time", "expgroup")) -> rich66

rich66
## # A tibble: 109 x 14
##    cdr3     cdrp quantity   fcp   fcq length    MW    AV    IP  flex   gravy
##    <chr>   <dbl>    <int> <dbl> <dbl>  <int> <dbl> <dbl> <dbl> <dbl>   <dbl>
##  1 GGVN… 0.0112       620  252.  310       8  889. 0.125  4.05 0.780 -1.44  
##  2 APAG… 0.00456      252  205.  252       9 1025. 0.222  4.37 0.760 -0.933 
##  3 PLTG… 0.00420      232  189.  232       7  800. 0.143  7.17 0.717  0.0571
##  4 VQES… 0.00371      205  167.  205      10 1133. 0.1    4.05 0.760 -0.09  
##  5 DPGH… 0.00351      194  158.  194       8  875. 0.125  4.05 0.807 -2.17  
##  6 DREV… 0.00337      186  151.  186      15 1834. 0.133  4.72 0.762 -1.24  
##  7 DIRW… 0.00322      178  145.  178      14 1747. 0.214  4.54 0.751 -0.993 
##  8 ETWG… 0.00610      337  137.  168.      7  881. 0.286  4.05 0.774 -1.7   
##  9 EDGY… 0.00280      155  126.  155      10 1173. 0.4    4.05 0.735 -1.42  
## 10 DLHW… 0.00277      153  124.  153       9 1065. 0.111  5.05 0.752 -0.833 
## # … with 99 more rows, and 3 more variables: SSF_Helix <dbl>, SSF_Turn <dbl>,
## #   SSF_Sheet <dbl>
names(cdr)
##  [1] "cdr3"      "cycle"     "time"      "expgroup"  "cdrp"      "quantity" 
##  [7] "fcp"       "fcq"       "length"    "MW"        "AV"        "IP"       
## [13] "flex"      "gravy"     "SSF_Helix" "SSF_Turn"  "SSF_Sheet" "n_A"      
## [19] "n_C"       "n_D"       "n_E"       "n_F"       "n_G"       "n_H"      
## [25] "n_I"       "n_K"       "n_L"       "n_M"       "n_N"       "n_P"      
## [31] "n_Q"       "n_R"       "n_S"       "n_T"       "n_V"       "n_W"      
## [37] "n_Y"       "aliphatic" "aromatic"  "neutral"   "positive"  "negative" 
## [43] "invalid"   "file"      "threshold" "rich"
cdr %>% 
  filter(cycle == "R4") %>% 
  ggplot() +
  geom_violin(aes(rich, gravy, color = rich)) +
  geom_jitter(aes(rich, gravy), alpha = .1)

2 Tentativa de Clusterização

2.1 Clusterization with Quantity and Prevalence variables

library(Rtsne)

set.seed(42)
tsne_df <- cdr %>% 
  filter(time == "final") %>% 
  filter(cycle == "R4")


tsne_df %>% 
  summarise(across(everything(), ~ all(sum(is.na(.x))))) %>% 
  select(where(isTRUE))
## # A tibble: 1 x 2
## # Groups:   expgroup, cycle [1]
##   expgroup cycle
##   <chr>    <chr>
## 1 rafael   R4
tsne_df %>% 
  filter(is.na(threshold)) %>% 
  select(rich, threshold, quantity)
## # A tibble: 0 x 6
## # Groups:   expgroup, cycle, time [0]
## # … with 6 variables: expgroup <chr>, cycle <chr>, time <chr>, rich <fct>,
## #   threshold <dbl>, quantity <int>
set.seed(42)
tsne_out <- 
  tsne_df %>% 
    ungroup() %>% 
    select(!where(is.character)) %>% 
    select(!c(which(apply(., 2, var)==0))) %>% 
    unique() %>% 
    Rtsne(
      X = .,
      dims = 3,
      perplexity = 30,
      theta = 0.5,
      max_iter = 1E3,
      verbose = T,
      pca_center = T,
      pca_scale = T,
      # partial_pca = T,
      normalize = T,
      eta = 200.0,
      exaggeration_factor = 12.0,
      num_threads = parallel::detectCores()
      )
## Performing PCA
## Read the 5684 x 42 data matrix successfully!
## OpenMP is working. 8 threads.
## Using no_dims = 3, perplexity = 30.000000, and theta = 0.500000
## Computing input similarities...
## Building tree...
## Done in 7.41 seconds (sparsity = 0.023010)!
## Learning embedding...
## Iteration 50: error is 90.878403 (50 iterations in 20.01 seconds)
## Iteration 100: error is 87.416493 (50 iterations in 14.37 seconds)
## Iteration 150: error is 86.914374 (50 iterations in 12.42 seconds)
## Iteration 200: error is 86.814880 (50 iterations in 11.74 seconds)
## Iteration 250: error is 86.763858 (50 iterations in 11.32 seconds)
## Iteration 300: error is 2.619334 (50 iterations in 12.31 seconds)
## Iteration 350: error is 2.144671 (50 iterations in 10.42 seconds)
## Iteration 400: error is 1.925249 (50 iterations in 10.57 seconds)
## Iteration 450: error is 1.796627 (50 iterations in 11.97 seconds)
## Iteration 500: error is 1.713050 (50 iterations in 10.91 seconds)
## Iteration 550: error is 1.658394 (50 iterations in 11.48 seconds)
## Iteration 600: error is 1.622142 (50 iterations in 12.35 seconds)
## Iteration 650: error is 1.595243 (50 iterations in 10.85 seconds)
## Iteration 700: error is 1.577939 (50 iterations in 11.29 seconds)
## Iteration 750: error is 1.566228 (50 iterations in 11.54 seconds)
## Iteration 800: error is 1.557336 (50 iterations in 11.24 seconds)
## Iteration 850: error is 1.549330 (50 iterations in 11.46 seconds)
## Iteration 900: error is 1.542255 (50 iterations in 11.90 seconds)
## Iteration 950: error is 1.536608 (50 iterations in 11.45 seconds)
## Iteration 1000: error is 1.531498 (50 iterations in 11.39 seconds)
## Fitting performed in 240.98 seconds.
tsne_df %>% 
  ungroup() %>% 
  select(!where(is.character)) %>% 
  select(!c(which(apply(., 2, var)==0))) %>% 
  unique() -> a

tsne_out %>% 
  .$Y %>% 
  as_tibble() %>% 
  ggplot() +
    geom_point(aes(V1, V2, color = a$rich))

tsne_out %>% 
  .$Y %>% 
  as_tibble() %>% 
  plot_ly(
    title = "Sample title",
    x = .$V1,
    y = .$V2,
    z = .$V3,
    type = "scatter3d",
    mode = "markers",
    color = a$rich
  ) %>% 
  layout(title = "With Quantity Variables")

2.2 Clusterization only with Biochemistry Properties

library(Rtsne)

set.seed(42)
tsne_df <- cdr %>% 
  filter(time == "final") %>% 
  filter(cycle == "R4")

tsne_df %>% 
  summarise(across(everything(), ~ all(sum(is.na(.x))))) %>% 
  select(where(isTRUE))
## # A tibble: 1 x 2
## # Groups:   expgroup, cycle [1]
##   expgroup cycle
##   <chr>    <chr>
## 1 rafael   R4
tsne_df %>% 
  filter(is.na(threshold)) %>% 
  select(rich, threshold, quantity)
## # A tibble: 0 x 6
## # Groups:   expgroup, cycle, time [0]
## # … with 6 variables: expgroup <chr>, cycle <chr>, time <chr>, rich <fct>,
## #   threshold <dbl>, quantity <int>
set.seed(42)
tsne_out <- 
  tsne_df %>% 
    ungroup() %>% 
    select(cdr3, !where(is.character)) %>% 
    select(!c(which(apply(., 2, var)==0))) %>% 
    select(!c("quantity", "cdrp", "fcp", "fcq", "rich", "threshold")) %>% 
    mutate(cdr3 = as.factor(cdr3)) %>% 
    Rtsne(
      X = .,
      dims = 3,
      perplexity = 30,
      theta = 0.5,
      max_iter = 1E3,
      verbose = T,
      pca_center = T,
      pca_scale = T,
      normalize = T,
      partial_pca = T,
      eta = 200.0,
      exaggeration_factor = 12.0,
      num_threads = parallel::detectCores()
      )
## Performing PCA
## Read the 5753 x 50 data matrix successfully!
## OpenMP is working. 8 threads.
## Using no_dims = 3, perplexity = 30.000000, and theta = 0.500000
## Computing input similarities...
## Building tree...
## Done in 8.83 seconds (sparsity = 0.024285)!
## Learning embedding...
## Iteration 50: error is 89.876509 (50 iterations in 13.17 seconds)
## Iteration 100: error is 89.838613 (50 iterations in 18.20 seconds)
## Iteration 150: error is 89.539497 (50 iterations in 14.79 seconds)
## Iteration 200: error is 89.513348 (50 iterations in 13.55 seconds)
## Iteration 250: error is 89.493390 (50 iterations in 13.23 seconds)
## Iteration 300: error is 3.177528 (50 iterations in 11.87 seconds)
## Iteration 350: error is 2.706362 (50 iterations in 11.36 seconds)
## Iteration 400: error is 2.501842 (50 iterations in 10.47 seconds)
## Iteration 450: error is 2.384828 (50 iterations in 11.75 seconds)
## Iteration 500: error is 2.309165 (50 iterations in 11.86 seconds)
## Iteration 550: error is 2.256507 (50 iterations in 10.81 seconds)
## Iteration 600: error is 2.219267 (50 iterations in 10.78 seconds)
## Iteration 650: error is 2.192489 (50 iterations in 11.42 seconds)
## Iteration 700: error is 2.175055 (50 iterations in 10.53 seconds)
## Iteration 750: error is 2.163736 (50 iterations in 10.42 seconds)
## Iteration 800: error is 2.155068 (50 iterations in 11.13 seconds)
## Iteration 850: error is 2.148645 (50 iterations in 10.90 seconds)
## Iteration 900: error is 2.143209 (50 iterations in 10.75 seconds)
## Iteration 950: error is 2.138527 (50 iterations in 10.71 seconds)
## Iteration 1000: error is 2.134156 (50 iterations in 11.91 seconds)
## Fitting performed in 239.61 seconds.
tsne_df %>% 
  ungroup() %>% 
  select(cdr3, !where(is.character)) %>% 
  select(!c(which(apply(., 2, var)==0))) %>% 
  select(!c("quantity", "cdrp", "fcp", "fcq", "threshold")) -> a

tsne_out %>% 
  .$Y %>% 
  as_tibble() %>% 
  ggplot() +
    geom_point(aes(V1, V2, color = a$rich))

tsne_out %>% 
  .$Y %>% 
  as_tibble() %>% 
  plot_ly(
    x = .$V1,
    y = .$V2,
    z = .$V3,
    type = "scatter3d",
    mode = "markers",
    color = a$rich
  ) %>% 
  layout(title = "Without Quantity Variables")